package com.zzh.net.Interceptor;


import com.zzh.net.HttpBaseUrl;
import com.zzh.net.NetConstants;

import java.io.IOException;
import java.util.List;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class BaseUrlInterceptor implements Interceptor {


    @Override
    public Response intercept(Chain chain) throws IOException {

        //获取request
        Request request = chain.request();
        //从request中获取原有的HttpUrl实例oldHttpUrl
        HttpUrl oldHttpUrl = request.url();
        //获取request的创建者builder
        Request.Builder builder = request.newBuilder();
        //从request中获取headers，通过给定的键url_name
        List<String> headerValues = request.headers(NetConstants.BASE_TYPE);
        if (headerValues != null && headerValues.size() > 0) {
            //如果有这个header，先将配置的header删除，因此header仅用作app和okhttp之间使用
            builder.removeHeader(NetConstants.BASE_TYPE);
            //匹配获得新的BaseUrl
            String headerValue = headerValues.get(0);
            HttpUrl newBaseUrl = null;
            switch (headerValue) {
                case NetConstants.HTTP_ADDRESS_REST_Fx:
                    newBaseUrl = HttpUrl.parse(HttpBaseUrl.HTTP_ADDRESS_REST_Fx);
                    break;
                case NetConstants.HTTPS_ADDRESS_REST_FX:
                    newBaseUrl = HttpUrl.parse(HttpBaseUrl.HTTPS_ADDRESS_REST_FX);
                    break;
                case NetConstants.HTTP_ADDRESS_REST:
                    newBaseUrl = HttpUrl.parse(HttpBaseUrl.HTTP_ADDRESS_REST);
                    break;
                case NetConstants.HTTP_ADDRESS_PHOTO:
                    newBaseUrl = HttpUrl.parse(HttpBaseUrl.HTTP_ADDRESS_PHOTO);
                    break;
                case NetConstants.HTTP_ADDRESS_H5:
                    newBaseUrl = HttpUrl.parse(HttpBaseUrl.HTTP_ADDRESS_H5);
                    break;
                case NetConstants.HTTP_ADDRESS_PAY:
                    newBaseUrl = HttpUrl.parse(HttpBaseUrl.HTTP_ADDRESS_PAY);
                    break;
                case NetConstants.HTTP_ADDRESS_IM:
                    newBaseUrl = HttpUrl.parse(HttpBaseUrl.HTTP_ADDRESS_IM);
                    break;
                case NetConstants.HTTP_SHARE_ADDRESS_H5:
                    newBaseUrl = HttpUrl.parse(HttpBaseUrl.HTTP_SHARE_ADDRESS_H5);
                    break;
                case NetConstants.HTTP_ERROR_COLLECT:
                    newBaseUrl = HttpUrl.parse(HttpBaseUrl.HTTP_ERROR_COLLECT);
                    break;
                case NetConstants.CHANGE_SHORT_URL:
                    newBaseUrl = HttpUrl.parse(HttpBaseUrl.CHANGE_SHORT_URL);
                    break;
                default:
                    newBaseUrl = HttpUrl.parse(HttpBaseUrl.HTTP_ADDRESS_REST_Fx);
                    break;

            }

            //重建新的HttpUrl，修改需要修改的url部分
            HttpUrl newFullUrl = oldHttpUrl
                    .newBuilder()
                    .scheme("https")//更换网络协议
                    .host(newBaseUrl.host())//更换主机名
                    .port(newBaseUrl.port())//更换端口
                    .build();
            //重建这个request，通过builder.url(newFullUrl).build()；
            // 然后返回一个response至此结束修改
            return chain.proceed(builder.url(newFullUrl).build());
        }
        return chain.proceed(request);
    }

}
